﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AlgoTest
{
    public class HighestNode
    {
        public class Node
        {
            public int item;
            public Node left;
            public Node right;
        };

        public static Node FindNextHighest(Node root, int i)
        {
            // return a node that is higher than i
            // i might or might not exist
            // return null if there is no next highest

            Node prevNode = null;

            while(root != null)
            {
                if (root.item <= i)
                {
                    root = root.right;
                }
                else
                {
                    prevNode = root;
                    root = root.left;
                }
            }

            return prevNode;
        }
    }
}
